import I18nApi from '@/api/basebiz/I18nApi'
import i18n from '@/i18n'
import store from '..'

export default {
  state: {
    downloaded: false,
    lanData: {},
    locale: 'zh-CN',
    loading: true
  },
  mutations: {
    SET_LANDATA(state, params) {
      state.downloaded = true
      state.lanData = params
      state.loading = false
    },
    SETLOCALE(state, params) {
      state.locale = params
    }
  },
  getters: {
    homeLoading: state => state.loading,
    getLocale: state => state.locale
  },
  actions: {
    loadLanData({ commit }) {
      I18nApi.allMap().then(res => {
        commit('SET_LANDATA', res)
        // console.log(res)
        for (const key in res) {
          if (Object.hasOwnProperty.call(res, key)) {
            i18n.setLocaleMessage(key, { ...i18n.messages[key], ...res[key] })
          }
        }
        store.commit('setDict')
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
